﻿using System;
using System.Collections.Generic;
using System.Text;

namespace BBMS.DLL.DataContract
{
    public enum PhuongThucThanhToan
    {
        TheoThoiHan = 0,
        TheoThoiDiem = 1
    }

    public enum TinhTrangNhaCungCap 
    {
        DangHoatDong = 0,
        TamDungThanhToan = 1,
        ChoThanhLy = 2,
        DaThanhLy = 3
    }
    public class NhaCungCap : BBMS.DLL.Base.ObjectBase
    {
        public string Ma;
        public string Ten;
        public DateTime NgayMo;
        public string DiaChi;
        public string ThanhPho;
        public TinhTrangNhaCungCap TinhTrang;
        public string MSTVAT;
        public string SoTK;
        public string TenNganHang;
        public string NguoiDaiDien;
        public string Email;
        public string SoDienThoai;
        public string Fax;
        public string MatHang;
        public long HanMuc;
        public PhuongThucThanhToan PhuongThucTT;
        public int ThoiHanTT = 0;
        public string ThoiDiemTT = "";
        public string LoaiNCC;
        public string TenVietTat;
        public string QuayList;

        public NhaCungCap()
        {
        }

        public NhaCungCap(string ma, string ten, string tenVietTat, DateTime ngayMo, string diachi, string thanhpho,
            TinhTrangNhaCungCap tinhtrang, string mstVAT, 
            string soTK, string nganhang, string nguoidaidien, string email, 
            string soDT, string fax, string mathang, PhuongThucThanhToan phuongthucTT,
            int thoiHanTT,
            string thoiDiemTT,
            long hanmuc,
            string loaiNCC, 
            string quayList)
        {
            this.Ma = ma;
            this.TenVietTat = tenVietTat;
            this.Ten = ten;
            this.NgayMo = ngayMo;
            this.DiaChi = diachi;
            this.ThanhPho = thanhpho;
            this.TinhTrang = tinhtrang;
            this.MSTVAT = mstVAT;
            this.SoTK = soTK;
            this.TenNganHang = nganhang;
            this.NguoiDaiDien = nguoidaidien;
            this.Email = email;
            this.SoDienThoai = soDT;
            this.Fax = fax;
            this.MatHang = mathang;
            this.PhuongThucTT = phuongthucTT;
            this.ThoiHanTT = thoiHanTT;
            this.ThoiDiemTT = thoiDiemTT;
            this.HanMuc = hanmuc;
            this.LoaiNCC = loaiNCC;
            this.QuayList = quayList;
        }

        public override object Key
        {
            get
            {
                return this.Ma;
            }
        }
        public override string Name
        {
            get
            {
                return this.Ten;
            }
        }
        public override string TableName
        {
            get
            {
                return "Supplier";
            }
        }

        public override string[] KeyFields
        {
            get
            {
                return new string[] { "Ma" };
            }
        }
        public override string[] IDFields
        {
            get
            {
                return new string[] { "Ma" };
            }
        }
        public override System.Data.DataTable GetObjectDataTable()
        {
            System.Data.DataTable dt = new System.Data.DataTable(this.TableName);
            dt.Columns.Add("Ma", typeof(String));
            dt.Columns.Add("Ten", typeof(String));
            dt.Columns.Add("TenVietTat", typeof(String));
            dt.Columns.Add("NgayMo", typeof(DateTime));
            dt.Columns.Add("DiaChi", typeof(String));
            dt.Columns.Add("ThanhPho", typeof(String));
            dt.Columns.Add("TinhTrang", typeof(int));
            dt.Columns.Add("MSTVAT", typeof(String));
            dt.Columns.Add("SoTK", typeof(String));
            dt.Columns.Add("TenNganHang", typeof(String));
            dt.Columns.Add("NguoiDaiDien", typeof(String));
            dt.Columns.Add("Email", typeof(String));
            dt.Columns.Add("SoDienThoai", typeof(String));
            dt.Columns.Add("Fax", typeof(String));
            dt.Columns.Add("MatHang", typeof(String));
            dt.Columns.Add("PhuongThucTT", typeof(int));
            dt.Columns.Add("ThoiHanTT", typeof(int));
            dt.Columns.Add("ThoiDiemTT", typeof(string));
            dt.Columns.Add("HanMuc", typeof(long));
            dt.Columns.Add("LoaiNCC", typeof(string));
            dt.Columns.Add("QuayList", typeof(string));
            return dt;
        }
        public override System.Data.DataRow ObjectToDataRow()
        {
            System.Data.DataRow row = this.GetObjectDataTable().NewRow();
            row["Ma"] = this.Ma;
            row["Ten"] = this.Ten;
            row["TenVietTat"] = this.TenVietTat;
            row["NgayMo"] = this.NgayMo;
            row["DiaChi"] = this.DiaChi;
            row["ThanhPho"] = this.ThanhPho;
            row["TinhTrang"] = (int)this.TinhTrang;
            row["MSTVAT"] = this.MSTVAT;
            row["SoTK"] = this.SoTK;
            row["TenNganHang"] = this.TenNganHang;
            row["NguoiDaiDien"] = this.NguoiDaiDien;
            row["Email"] = this.Email;
            row["SoDienThoai"] = this.SoDienThoai;
            row["Fax"] = this.Fax;
            row["MatHang"] = this.MatHang;
            row["PhuongThucTT"] = (int)this.PhuongThucTT;
            row["ThoiHanTT"] = this.ThoiHanTT;
            row["ThoiDiemTT"] = this.ThoiDiemTT;
            row["HanMuc"] = this.HanMuc;
            row["LoaiNCC"] = this.LoaiNCC;
            row["QuayList"] = this.QuayList;
            return row;
        }

        public override BBMS.DLL.Base.ObjectBase DataRowToObject(System.Data.DataRow row)
        {
            NhaCungCap obj = null;
            try
            {
                obj = new NhaCungCap(row["Ma"].ToString(), 
                    row["Ten"].ToString(),
                    row["TenVietTat"].ToString(),
                    (DateTime)row["NgayMo"],
                    row["DiaChi"].ToString(),
                    row["ThanhPho"].ToString(),
                    (TinhTrangNhaCungCap)CommonTools.Helper.StringToInt(row["TinhTrang"].ToString()),
                    row["MSTVAT"].ToString(),
                    row["SoTK"].ToString(),
                    row["TenNganHang"].ToString(),
                    row["NguoiDaiDien"].ToString(),
                    row["Email"].ToString(),
                    row["SoDienThoai"].ToString(),
                    row["Fax"].ToString(),
                    row["MatHang"].ToString(),
                    (PhuongThucThanhToan)CommonTools.Helper.StringToInt(row["PhuongThucTT"].ToString()),
                    CommonTools.Helper.StringToInt(row["ThoiHanTT"].ToString()),
                    row["ThoiDiemTT"].ToString(),
                    CommonTools.Helper.StringToLong(row["HanMuc"].ToString()),
                    row["LoaiNCC"].ToString(),
                    row["QuayList"].ToString());
            }
            catch { }
            return obj;
        }

        public override BBMS.DLL.Base.ObjectBase DataReaderToObject(MySql.Data.MySqlClient.MySqlDataReader rd)
        {
            NhaCungCap obj = null;
            try
            {
                
                obj = new NhaCungCap(rd["Ma"].ToString(),
                    rd["Ten"].ToString(),
                    rd["TenVietTat"].ToString(),
                    (DateTime)rd["NgayMo"],
                    rd["DiaChi"].ToString(),
                    rd["ThanhPho"].ToString(),
                    (TinhTrangNhaCungCap)CommonTools.Helper.StringToInt(rd["TinhTrang"].ToString()),
                    rd["MSTVAT"].ToString(),
                    rd["SoTK"].ToString(),
                    rd["TenNganHang"].ToString(),
                    rd["NguoiDaiDien"].ToString(),
                    rd["Email"].ToString(),
                    rd["SoDienThoai"].ToString(),
                    rd["Fax"].ToString(),
                    rd["MatHang"].ToString(),
                    (PhuongThucThanhToan)CommonTools.Helper.StringToInt(rd["PhuongThucTT"].ToString()),
                    CommonTools.Helper.StringToInt(rd["ThoiHanTT"].ToString()),
                    rd["ThoiDiemTT"].ToString(),
                    CommonTools.Helper.StringToLong(rd["HanMuc"].ToString()),
                    rd["LoaiNCC"].ToString(),
                    rd["QuayList"].ToString());
            }
            catch { }
            return obj;
        }
        public override string InsertNew()
        {
            string str = base.InsertNew();
            if (str == "") ObjectLists.NhaCungCapList.Add(this);
            return str;
        }
        public override string Delete()
        {
            string str = base.Delete();
            if (str == "") ObjectLists.NhaCungCapList.Remove(this);
            return str;
        }
        public override string Save()
        {
            string str = base.Save();
            if (str == "") ObjectLists.NhaCungCapList.Update(this);
            return str;
        }
        public static bool IsValidThoiDiemTT(string str)
        {
            str = str.Replace(" ","");
            str = str.Replace("\t","");
            bool ret = true;
            string[] strs = str.Split(',');
            for (int i = 0; i < strs.Length; i++)
            {
                int ngay = CommonTools.Helper.StringToInt(strs[i]);
                if (ngay < 0 || ngay > 31)
                {
                    ret = false;
                    break;
                }
            }
            return ret;
        }
    }
}
